package com.kelp.codetoolkit.renderer

import com.intellij.ui.JBColor
import com.intellij.util.ui.ImageUtil
import com.kelp.codetoolkit.data.ListCellData
import java.awt.Component
import java.awt.Dimension
import java.awt.Font
import java.awt.Image
import java.awt.image.BufferedImage
import javax.swing.*


/**
 * @author: liuyuebai
 * @date: 2024/11/19 19:24
 * @description:
 */
class ParamClassListCellRenderer: DefaultListCellRenderer() {

    private var paramScrollPane: JScrollPane? = null
    fun setParamScrollPane(scrollPane: JScrollPane?) {
        this.paramScrollPane = scrollPane

    }

    override fun getListCellRendererComponent(list: JList<*>?, value: Any?, index: Int, isSelected: Boolean, cellHasFocus: Boolean): Component {
        val cellData = value as? ListCellData
        val psiClass = cellData?.psiClass
        if (psiClass != null) {
            val showClassPanel = JPanel()
            showClassPanel.layout = BoxLayout(showClassPanel, BoxLayout.X_AXIS)
            if (psiClass.isEnum){
                val enumResource = this.javaClass.getResource("/icons/enumeration.png")
                val enumIcon = ImageIcon(enumResource)
                val image: Image = enumIcon.getImage()
                val scaledImage = image.getScaledInstance(16, 16, Image.SCALE_SMOOTH)
                enumIcon.setImage(scaledImage)
                val enumIconLabel = JLabel(enumIcon)
                showClassPanel.add(enumIconLabel)
            }else if (psiClass.isInterface){
                val interfaceResource = this.javaClass.getResource("/icons/interface.png")
                val interfaceIcon = ImageIcon(interfaceResource)
                val image: Image = interfaceIcon.getImage()
                val scaledImage = image.getScaledInstance(16, 16, Image.SCALE_SMOOTH)
                interfaceIcon.setImage(scaledImage)
                val interfaceIconLabel = JLabel(interfaceIcon)
                showClassPanel.add(interfaceIconLabel)
            }else{
                val classResource = this.javaClass.getResource("/icons/class.png")
                val classIcon = ImageIcon(classResource)
                val image: Image = classIcon.getImage()
                val scaledImage = image.getScaledInstance(16, 16, Image.SCALE_SMOOTH)
                classIcon.setImage(scaledImage)
                val classIconLabel = JLabel(classIcon)
                showClassPanel.add(classIconLabel)
            }
            val firstBlankLabel = JLabel(" ")
            showClassPanel.add(firstBlankLabel)
            val paramClassNameLabel = JLabel(cellData.shortClassName)
            paramClassNameLabel.setFont(Font("Monospaced", Font.PLAIN, 18))
            showClassPanel.add(paramClassNameLabel)
            val twoBlankLabel = JLabel("  ")
            showClassPanel.add(twoBlankLabel)
            val packageName = cellData.packageName
            val packageNameLabel = JLabel(packageName)
            // 设置字体颜色为浅灰色
            packageNameLabel.setForeground(JBColor.GRAY);
            packageNameLabel.setFont(Font("Monospaced", Font.PLAIN, 18))
            showClassPanel.add(packageNameLabel)

            // 设置JList单元格的最大宽度为paramScrollPane的宽度
            showClassPanel.maximumSize = Dimension(paramScrollPane!!.viewport.width, showClassPanel.preferredSize.height)

            return showClassPanel
        }
        return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus)
    }
}